package com.br4mmie.asciistory.shop;

import com.br4mmie.asciistory.mission.*;

/**
 * Shop container with 2 inventories
 * @author Bram Buurlage
 * 		   Pim Korschner
 */

public class Shop 
{
	Inventory seller;
	Inventory buyer;
	
	/**
	 * constructor
	 * @param seller the inventory of the seller
	 * @param buyer  the inventory of the buyer (the player)
	 */
	public Shop(Inventory seller, Inventory buyer)
	{
		this.seller = seller;
		this.buyer = buyer;
	}
	
	/**
	 * sell an item to the shop
	 * @param item the item to sell
	 * @param gold the gold item to update
	 */
	public void sell(InventoryItem item, InventoryItem gold)
	{
		System.out.println("sell");
		seller.receiveItem(item.item);
		
		buyer.removeItem(item);
		buyer.receiveGold(item.item.getGoldValue());
		try{Thread.sleep(500);}catch(Exception e){e.printStackTrace();}
	}
	
	/**
	 * Buy an item from the shop
	 * @param item the item to buy
	 * @param gold the gold item to update
	 * @return null. oops.
	 */
	public InventoryItem buy(Item item, InventoryItem gold)
	{
		for (InventoryItem ii: seller)
		{
			if (ii.item.equals(item))
			{
				if (ii.item.getGoldValue() <= buyer.getGold())
				{
					seller.removeItem(ii);
					buyer.receiveItem(item);
					buyer.receiveGold(-item.getGoldValue());
				}
				try{Thread.sleep(500);}catch(Exception e){e.printStackTrace();}
				
				break;
			}
		}
		return null;
	}
}
